package com.hack.qiaoqiao;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;

public class MainActivity extends Activity {

	private ImageSwitcher mSwitcher;
	private ArrayList<String> mContents;
	private ArrayList<Integer> mImageIds;

	public void OnCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mSwitcher = (ImageSwitcher) findViewById(R.id.main_switcher);
		/**
		 * 20 : 写死最大记录数，可更改
		 */
		mSwitcher.setFactory(new ImageAdapter(getApplicationContext(), 20));

	}

	class ImageAdapter extends BaseAdapter implements ViewFactory {
		private int mGalleryItemBackground;
		// 定义map存储划过的位置
		private HashMap<Integer, View> mViewMaps;
		private int mCount;
		// 定义布局加载器
		private LayoutInflater mInflater;

		public ImageAdapter(Context c, int count) {
			this.mCount = count;
			mViewMaps = new HashMap<Integer, View>(count);
			mInflater = LayoutInflater.from(MainActivity.this);
		}

		public int getCount() {
			// 设置循环的次数
			return Integer.MAX_VALUE;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			Log.v("TAG", "getView() position=" + position + " convertView="
					+ convertView);
			View viewGroup = mViewMaps.get(position % mCount);
			ImageView imageView = null;
			TextView textView = null;
			if (viewGroup == null) {
				viewGroup = mInflater.inflate(R.layout.switcher_items, null);
				imageView = (ImageView) viewGroup
						.findViewById(R.id.switcher_image);
				textView = (TextView) viewGroup
						.findViewById(R.id.switcher_text);
				imageView.setBackgroundResource(mGalleryItemBackground);
				mViewMaps.put(position % mCount, viewGroup);
				imageView.setImageResource(mImageIds.get(position
						% mImageIds.size()));
				textView.setText(mContents.get(position % mImageIds.size()));
			}

			return viewGroup;
		}

		@Override
		public View makeView() {
			// TODO Auto-generated method stub
			return null;
		}
	}
}
